package it.polito.let.jhp.form;

import java.util.HashMap;
import java.util.Map;

public class FormUtil {

	public static void genHtmlRenderType(JHPForm curForm, Map<String, JHPForm> formsMap, Object[] t)
	{
		//Object[] o = {tipo,id,mappa,tagapertura}; 
		// tipi tag: 0: roba che non richiede nulla ( <p>, href ... ) 1: tipo input 2: textarea

		String s;

		switch((Integer)t[0])
		{

		case 0:
			s = (String)t[3]+(String)t[1]+attrToMap(t[2])+">";
			curForm.addHtml(s);
			break;

		case 1:

			break;

		case 2:
			s = (String)t[3]+(String)t[1]+attrToMap(t[2])+">";
			curForm.addHtml(s);
			//	 curForm.
			break;
		}

	}

	private static String attrToMap(Object o) {
		if(o== null)
			return "";
		HashMap<String,String> m = (HashMap<String,String>) o;
		StringBuffer s = new StringBuffer();
		for ( String i:m.keySet())
		{
			s.append(" "+i + "=" + m.get(i));
		}
		return s.toString();
	}

	public static boolean isHtmlInputType(String type) {
		return type.matches("text|password|checkbox|radio|submit|reset|file|hidden|image|button");
	}

}
